package com.xucy.springboot.redis;

import com.xucy.springboot.entity.Person;
import com.xucy.springboot.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xucy
 * @Date 2019-07-01 20:01
 * @Description
 **/

@RestController
@RequestMapping("/redisTeCon")
public class RedisTeController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private PersonService personService;

    /**
     * 测试redis
     *
     * @param id 根据不同的id来判断是对哪种类型数据进行操作
     * @return
     * @Author xucy
     * @Date 09:27 2019-07-02
     */
    @RequestMapping("/test")
    public String test(String id) {

        if (id.equals("string")) {
            testString(id);
        }else if (id.equals("list")) {
            testList(id);
        }else if(id.equals("hash")){
            testHash(id);
        }else if(id.equals("object")){
            testObject(id);
        }

        return "成功";
    }

    //测试string
    public void testString(String id) {
        // 保存字符串
        List<Person> studentList = personService.findAllPerson();
        if (studentList != null) {
            for (int i = 0; i < studentList.size(); i++) {
                Person person = studentList.get(i);
                stringRedisTemplate.opsForValue().set("person" + i, person.getName());

                if (stringRedisTemplate.hasKey("person" + i)) {
                    String value = stringRedisTemplate.opsForValue().get("person" + i);
                    System.out.println(value);

                    //在存在key的情况下 追加，否则与set一样
                    stringRedisTemplate.opsForValue().append("person" + i,value);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    stringRedisTemplate.delete("person" + i);
                }
            }

            //存多个值
            Map<String,String> maps=new HashMap<>();
            maps.put("map1","value1");
            maps.put("map2","value2");
            maps.put("map3","value3");
            stringRedisTemplate.opsForValue().multiSet(maps);

            //如果不存在就添加
            stringRedisTemplate.opsForValue().setIfAbsent("map1","value2");

            //存入新值，并返回旧值
            stringRedisTemplate.opsForValue().getAndSet("map2","value3");
            List list=new ArrayList();
            list.add("map1");
            list.add("map2");
            list.add("map3");
            List result=stringRedisTemplate.opsForValue().multiGet(list);
            System.out.println(result.size());
            System.out.println(list.get(0));
        }
    }

    //测试存list
    public void testList(String id) {
        redisTemplate.opsForList().leftPush("list1","1");
        redisTemplate.opsForList().leftPush("list1","2");
        redisTemplate.opsForList().leftPush("list1","3");
        redisTemplate.opsForList().leftPush("list1","4");

        List list= redisTemplate.opsForList().range("list1",0,-1);
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
        redisTemplate.delete("list1");
        System.out.println("----------------------------------------------");

        stringRedisTemplate.opsForList().rightPush("list2","1");
        stringRedisTemplate.opsForList().rightPush("list2","2");
        stringRedisTemplate.opsForList().rightPush("list2","3");
        stringRedisTemplate.opsForList().rightPush("list2","4");

        List list1= stringRedisTemplate.opsForList().range("list2",0,-1);
        for(int i=0;i<list1.size();i++){
            System.out.println(list1.get(i));
        }

        //弹出
        String value=stringRedisTemplate.opsForList().leftPop("list2");
        System.out.println("弹出"+value);

        stringRedisTemplate.opsForList().trim("list2",2,-1);

        stringRedisTemplate.delete("list2");
    }

    //测试存hash
    public void testHash(String id) {
        stringRedisTemplate.opsForHash().put("hash1","age","18");
        stringRedisTemplate.opsForHash().put("hash1","name","徐晨阳");
        stringRedisTemplate.opsForHash().put("hash1","sex","男");

        String value= (String) stringRedisTemplate.opsForHash().get("hash1","age");
        String value1= (String) stringRedisTemplate.opsForHash().get("hash1","name");
        String value2= (String) stringRedisTemplate.opsForHash().get("hash1","sex");
        System.out.println(value);
        System.out.println(value1);
        System.out.println(value2);


        Map<Object, Object> hash1 = stringRedisTemplate.opsForHash().entries("hash1");
        System.out.println(hash1.get("age"));
        System.out.println(hash1.get("name"));
        System.out.println(hash1.get("sex"));
        stringRedisTemplate.opsForHash().increment("hash1","age",20);
        String value4= (String) stringRedisTemplate.opsForHash().get("hash1","age");
        System.out.println(value4);
        boolean flag= stringRedisTemplate.opsForHash().hasKey("hash1","age");
        System.out.println(flag);
    }

    //添加对象
    public void testObject(String id){
        Person person=new Person();
        person.setId(3);
        person.setName("杨戬");
        person.setAge("71");
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.opsForValue().set("yang",person);

        Person person1=(Person)redisTemplate.opsForValue().get("yang");
        System.out.println(person1.getAge()+"--"+person1.getName());
        redisTemplate.delete("yang");
    }
}